<?php

namespace App;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //

    protected $guarded=[];
    //globa  scope
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('status', function($builder) {
            $builder->whereIn('status', [0,1]);
        });
    }
 
    public function  post_topic(){
                return $this->hasMany('App\PostTopic');
    }
  // 属于作者的文章
    public function scopeAu($query,$user_id){
            return $query->where('user_id',$user_id);
    }
    //不属于某个专题的文章
    public function scopeTopics($query,$topic_id){
        return $query->doesntHave('post_topic','and',function($q) use($topic_id){

                $q->where('topic_id',$topic_id);

        });
    }
    //属于某个专题的文章
     public function scopePan($query,$topic_id){
        return $query->whereHas('post_topic',function($q) use($topic_id){

                $q->where('topic_id',$topic_id);

        });
    }
    public function comments(){
    		return $this->hasMany('App\Comment')->orderBy('created_at','desc');
    }
    public function user(){


     return $this->belongsTo('App\User');

    }
     public function zan(){


     return $this->hasMany('App\Zan');

    }
     public function unzan($id){


     return $this->hasone('App\Zan')->where(['user_id'=>$id]);

    }


}
